﻿using MB.Orm.DB;
using MB.RuleBase.Common;
using Model;
using Public.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.Service
{
    public class BAreaPort
    { /// <summary>
        /// 新增关联信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddAreaSendPort(AreaPort model)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<AreaPort>
                            ("AreaPort", "AddAreaSendPort", model);
        }
        /// <summary>
        /// 更新关联信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditAreaSendPort(AreaPort model)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<AreaPort>
                            ("AreaPort", "EditAreaSendPortByID", model);
        }
        /// <summary>
        /// 删除关联信息
        /// </summary>
        /// <param name="AreaPortID"></param>
        /// <returns></returns>
        public int DeleteAreaSendPort(int AreaPortID)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("AreaPort", "DeleteAreaSendPort", AreaPortID);
        }

        /// <summary>
        /// 根据主键ID获取发货口和区域对应关系
        /// </summary>
        /// <param name="areaID"></param>
        /// <returns></returns>
        public AreaPort GetAreaPortByID(string areaportID)
        {
            areaportID = string.IsNullOrEmpty(areaportID) ? "0" : areaportID;
            AreaPort Info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<AreaPort>("AreaPort", "CheckAreaSendPort", areaportID).FirstOrDefault();
            return Info;
        }

        /// <summary>
        /// 根据区域ID获取发货口和区域对应关系
        /// </summary>
        /// <param name="areaID"></param>
        /// <returns></returns>
        public AreaPort GetAreaPortByAreaID(string areaID)
        {
            areaID = string.IsNullOrEmpty(areaID) ? "0" : areaID;
            AreaPort Info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<AreaPort>("AreaPort", "GetAreaSendPortByAreaID", areaID).FirstOrDefault();
            return Info;
        }
        /// <summary>
        /// 根据区域ID（排除当前一条）获取发货口和区域对应关系
        /// </summary>
        /// <param name="areaID"></param>
        /// <returns></returns>
        public AreaPort GetAreaPortByAreaIDWithoutID(string areaID, string areaportID)
        {
            areaID = string.IsNullOrEmpty(areaID) ? "0" : areaID;
            areaportID = string.IsNullOrEmpty(areaportID) ? "0" : areaportID;
            AreaPort Info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<AreaPort>("AreaPort", "GetAreaSendPortByAreaIDWithoutID", areaID, areaportID).FirstOrDefault();
            return Info;
        }

        /// <summary>
        /// 获取大区列表
        /// </summary>
        /// <param name="IsSetAll"></param>
        /// <returns></returns>
        public List<lookupitem> GetAreaList(int IsSetAll)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<lookupitem> Arealist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<lookupitem>("AreaPort", "GetAreaList", "");
                if (IsSetAll <= 0)
                {
                    lookupitem w = new lookupitem();
                    w.lookupid = "";
                    w.fieldvalue = "0";
                    w.lookup = "--全部--";
                    Arealist.Insert(0, w);
                }
                return Arealist;
            }
        }

        /// <summary>
        /// 获取发货口列表
        /// </summary>
        /// <param name="IsSetAll"></param>
        /// <returns></returns>
        public List<lookupitem> GetSendPortList(int IsSetAll)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<lookupitem> SendPortlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<lookupitem>("AreaPort", "GetSendPortList", "");
                if (IsSetAll <= 0)
                {
                    lookupitem w = new lookupitem();
                    w.lookupid = "";
                    w.fieldvalue = "0";
                    w.lookup = "--全部--";
                    SendPortlist.Insert(0, w);
                }
                return SendPortlist;
            }
        }

        /// <summary>
        /// 获取大区和发货口关联信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="parValues"></param>
        /// <returns></returns>
        public List<AreaPort> GetAreaSendPortList(int pageIndex, int pageSize, out int total, params object[] parValues)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<AreaPort> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml3<AreaPort>
                                                ("AreaPort", "GetAreaSendPortList", pageIndex, pageSize, out total, parValues);
                return list;
            }
        }
    }
}
